from flask_wtf import FlaskForm,RecaptchaField
from flask_wtf.file import FileField,file_required
from wtforms import StringField,PasswordField,SubmitField,BooleanField,TextAreaField
from wtforms.validators import DataRequired,Length,EqualTo,ValidationError
from models import User

class LoginForm(FlaskForm):
     username = StringField('Username', validators=[DataRequired(),Length(min=1,max=20)])
     password = PasswordField('Password', validators=[DataRequired()])   
     remember=BooleanField("Remember ME")
     submit=SubmitField("Login")

     def validate_username(self,username):  # 格式是固定的  validate_字段名
        u=User.query.filter_by(username=username.data).first()
        if not u:
            raise ValidationError("用户名不存在")

class RegisterForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired(),Length(min=3,max=20)])
    password = PasswordField('Password', validators=[DataRequired()])
    password2 = PasswordField('Confirm Password', validators=[DataRequired(),EqualTo('password')])
    email = StringField('Email', validators=[DataRequired()])
    # recaptcha=RecaptchaField()  # 需要梯子才能使用
    submit=SubmitField("Register")

    def validate_username(self,username):  # 格式是固定的  validate_字段名
        u=User.query.filter_by(username=username.data).first()
        if u:
            raise ValidationError("用户名已经存在")
    def validate_email(self,email):
        u=User.query.filter_by(email=email.data).first()
        if u:
            raise ValidationError("email已经存在")


class PostForm(FlaskForm):
    text= TextAreaField('More ...', validators=[DataRequired()])
    submit=SubmitField("Submit")

class EditForm(FlaskForm):
    img=FileField("Select Image File.",validators=[DataRequired()])
    submit=SubmitField("Submit")